package com.jxtti.controller;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.jxtti.entity.Admin_RoleMenu;
import com.jxtti.service.AdminRoleMenuService;
import com.jxtti.utils.ExceptionUtil;

@Controller
@RequestMapping("/AdminRoleMenu") 
public class AdminRoleMenuController {
	private final static Logger log= Logger.getLogger(AdminRoleMenuController.class);
	
	@Autowired 
	private AdminRoleMenuService  adminRoleMenuService; 
		
	@InitBinder  //初始化数据的时候，进行数据类型转换，String类型转为Date类型
    public void initBinder(WebDataBinder binder) {     
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");     
        dateFormat.setLenient(false);     
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true:���������ֵ��false:����Ϊ��ֵ 
    }
	
	/**
	 * 查询所有数据,
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/queryAll")
	@ResponseBody
	public List<Admin_RoleMenu> queryTCityAQIWriteAll(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) throws Exception{		
		
		List<Admin_RoleMenu>  Admin_RoleMenu= adminRoleMenuService.queryAll(data);
		return Admin_RoleMenu;
	}
	
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	@ResponseBody	
	public Map<String,Object> save(@RequestParam Map<String, Object> data,
			Admin_RoleMenu  admin_RoleMenu, HttpServletRequest request,	HttpServletResponse response) throws Exception{		
		ModelAndView mav = new ModelAndView();
		try {				
			//先从表中删除 该角色的所有菜单			
			String fk_RoleType=data.get("roleId").toString();
			admin_RoleMenu.setRoleId(Integer.parseInt(fk_RoleType));
			adminRoleMenuService.deleteByRole(admin_RoleMenu);			
			//保存 角色菜单
			String[] idArr=data.get("ids").toString().split(",");				
			for(String id:idArr){
				Admin_RoleMenu  RoleMenu=new Admin_RoleMenu();
				RoleMenu.setRoleId( Integer.parseInt(fk_RoleType));
				RoleMenu.setMenuId(Integer.parseInt(id));							
				int exeCount=adminRoleMenuService.add(RoleMenu);				
				if (exeCount > 0) {
					data.put("success", true);
				} else {
					data.put("success", false);
				}
			}			
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}	
		
	/**
	 * 修改信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			Admin_RoleMenu  Admin_RoleMenu, HttpServletRequest request,
			HttpServletResponse response) {
		try {
			//	User user = AuthUtil.getUserInfo(request);
			//	tSite.setOperPerson(user.getUserName());
			
			int exeCount;		
			exeCount = adminRoleMenuService.update(Admin_RoleMenu);			
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	/**
	 * 删除信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) {
		try {			
			String[] idArr=data.get("ids").toString().split(",");
			for(String id:idArr){
				Admin_RoleMenu  Admin_RoleMenu=new Admin_RoleMenu();
				Admin_RoleMenu.setId(Integer.parseInt(id));				
				Admin_RoleMenu=adminRoleMenuService.queryListById(Admin_RoleMenu);				
				int exeCount=adminRoleMenuService.delete(Admin_RoleMenu);
				if (exeCount > 0) {
					data.put("success", true);
				} else {
					data.put("success", false);
				}
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
}
